package me.qi.kancha.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.qi.kancha.dto.base.BaseDTO;
import me.qi.kancha.dto.core.ArticleDTO;
import me.qi.kancha.dto.enums.ArticleStatus;
import me.qi.kancha.service.ArticleService;
import me.qi.kancha.mapper.ArticleMapper;
import me.qi.kancha.service.base.support.PageData;
import me.qi.kancha.support.U;
import me.qi.kancha.vo.article.ArticleInsertVo;
import me.qi.kancha.vo.article.ArticleQueryVo;
import me.qi.kancha.vo.article.ArticleStatusChangeVo;
import me.qi.kancha.vo.article.ArticleUpdateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;

/**
* 文章表模块-service实现
*
* @author qi
* @date 2022-10-26 01:43:51
*/
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ArticleDTO>
    implements ArticleService{

    @Override
    public PageData<ArticleDTO> listPaging(Object paramObj) {
        ArticleQueryVo param = (ArticleQueryVo) paramObj;
        LambdaQueryWrapper<ArticleDTO> wrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(param.getStatus())) {
            wrapper.eq(ArticleDTO::getStatus, param.getStatus());
        }
        if (!ObjectUtils.isEmpty(param.getType1())) {
            wrapper.eq(ArticleDTO::getType1, param.getType1());
        }
        if (!ObjectUtils.isEmpty(param.getType2())) {
            wrapper.eq(ArticleDTO::getType2, param.getType2());
        }
        if (!ObjectUtils.isEmpty(param.getKey())) {
            wrapper.like(ArticleDTO::getTitle, param.getKey());
        }
        if (!ObjectUtils.isEmpty(param.getSource())) {
            wrapper.like(ArticleDTO::getSource, param.getSource());
        }
        Page page = page(param.pageInstance(), wrapper);
        return new PageData<>(page);
    }

    @Override
    public ArticleDTO insert(Object paramObj) {
        ArticleInsertVo param = (ArticleInsertVo) paramObj;
        ArticleDTO articleDTO = new ArticleDTO();
        BeanUtils.copyProperties(param, articleDTO);
        articleDTO.setView(0);
        articleDTO.setStatus(ArticleStatus.NORMAL);
        String txt = param.getContent().replaceAll("</?[^>]+>", "");
        txt = txt.replaceAll("<a>\\s*|\t|\r|\n</a>", "");
        articleDTO.setContentTxt(txt);
        save(articleDTO);
        return articleDTO;
    }

    @Override
    public ArticleDTO update(Object paramObj) {
        ArticleUpdateVo param = (ArticleUpdateVo) paramObj;
        ArticleDTO dto = getById(param.getId());
        BeanUtils.copyProperties(param, dto);
        String txt = dto.getContent().replaceAll("</?[^>]+>", "");
        txt = txt.replaceAll("<a>\\s*|\t|\r|\n</a>", "");
        dto.setContentTxt(txt);
        updateById(dto);
        return dto;
    }

    @Override
    public boolean statusChange(ArticleStatusChangeVo param) {
        return lambdaUpdate()
                .set(ArticleDTO::getStatus, param.getStatus())
                .eq(BaseDTO::getId, param.getId())
                .update();
    }
}




